package com.hi.hiparty.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hi.hiparty.entity.User;
import com.hi.hiparty.manager.UserManager;
import com.hi.hiparty.util.Constants;
import com.hi.hiparty.util.JSONResponseHelper;
import com.hi.hiparty.util.UpYunUtils;
import com.hi.hiparty.util.WebUtil;


@Controller
public class UserController {
	
	private static final Log logger = LogFactory.getLog(UserController.class);
	
	@Autowired
	private UserManager userManager;

	@RequestMapping(value = "/mobileapi/user/register/{loginname}/{password}")
	public void register(@PathVariable("loginname") String loginname,
			@PathVariable("password") String password,
			HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		if (0 == userManager.checkLoginName(loginname)) {
			int userid = userManager.addUser(loginname, password);
			result.put("success", 1);
			result.put("userid", userid);
		} else {
			result.put("success", 0);
			result.put("error", "用户名重复");
		}
		JSONResponseHelper.sendJSONResult(response, result, null);
	}

	@RequestMapping(value = "/mobileapi/user/checkname/{loginname}")
	public void checkLoginName(@PathVariable("loginname") String loginname,
			HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		if (StringUtils.isBlank(loginname)) {
			result.put("success", 0);
			result.put("error", "用户名不能为空");
		} else if (0 == userManager.checkLoginName(loginname)) {
			result.put("success", 1);
		} else {
			result.put("success", 0);
			result.put("error", "用户名重复");
		}
		JSONResponseHelper.sendJSONResult(response, result, null);
	}

	@RequestMapping(value = "/mobileapi/user/login/{loginname}/{password}")
	public void checkLogin(@PathVariable("loginname") String loginname,
			@PathVariable("password") String password,
			HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();

		if (StringUtils.isBlank(loginname) || StringUtils.isBlank(password)) {
			result.put("success", 0);
			result.put("error", "用户名或者密码不能为空");
			JSONResponseHelper.sendJSONResult(response, result, null);
			return;
		}

		User user = userManager.checkLogin(loginname, password);
		if (null == user) {
			result.put("success", 0);
			result.put("error", "用户名或者密码不正确");
			JSONResponseHelper.sendJSONResult(response, result, null);
			return;
		}
		result.put("success", 1);
		result.put("userid", user.getUserid());
		result.put("nickname", user.getNickname());
		result.put("gender", user.getGender());
		JSONResponseHelper.sendJSONResult(response, result, null);
	}

	@RequestMapping(value = "/mobileapi/user/nickname/update")
	public void updateNickname(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String nickname = request.getParameter("nickname");
		String gender = request.getParameter("gender");
		String userid = request.getParameter("userid");

		JSONObject result = new JSONObject();

		if (StringUtils.isBlank(userid)) {
			result.put("success", 0);
			result.put("error", "用户ID不可为空");
			JSONResponseHelper.sendJSONResult(response, result, null);
			return;
		}

		if (StringUtils.isBlank(nickname)) {
			result.put("success", 0);
			result.put("error", "用户昵称不可为空");
			JSONResponseHelper.sendJSONResult(response, result, null);
			return;
		}

		userManager.updateUserNicknameAndGender(userid,
				nickname, gender);
		
		result.put("success", 1);
		result.put("userid", userid);
		JSONResponseHelper.sendJSONResult(response, result, null);
	}
	
	@RequestMapping(value = "/mobileapi/user/avatar/update")
	public void updateUserHeadPic(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String userid = WebUtil.getStringPara(request, "userid");
		//目前使用又拍的图片路径
		String avatar = WebUtil.getStringPara(request, "avatar");
		JSONObject result = new JSONObject();
		if(userManager.updateUserAvatar(userid, avatar)){
			result.put("success", 1);
			JSONResponseHelper.sendJSONResult(response, result, null);
			return;
		}else{
			result.put("success", 0);
			JSONResponseHelper.sendJSONResult(response, result, null);
		}
	}
	
	@RequestMapping(value = "/mobileapi/user/get")
	public void getUserHeadPic(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		int userid = WebUtil.getIntPara(request, "userid");
		User user = userManager.getUser(userid);
		JSONObject result = new JSONObject();
		if(null != user){
			result.put("userid", user.getUserid());
			result.put("nickname", user.getNickname());
			result.put("gender", user.getGender());
			result.put("mood", user.getMood());
			result.put("interest", user.getInterest());
			result.put("career", user.getCareer());
			result.put("avatar", user.getAvatarUrl());
			result.put("success", 1);
			JSONResponseHelper.sendJSONResult(response, result, null);
			return;
		}else{
			result.put("success", 0);
			JSONResponseHelper.sendJSONResult(response, result, null);
		}
	}
	
	@RequestMapping(value = "/mobileapi/user/update")
	public void updateUser(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		int userid = WebUtil.getIntPara(request, "userid");
		String nickname = WebUtil.getStringPara(request, "nickname", null);
		int gender = WebUtil.getIntPara(request, "gender", 0);
		String mood = WebUtil.getStringPara(request, "mood", null);
		String interest = WebUtil.getStringPara(request, "interest", null);
		String career = WebUtil.getStringPara(request, "career", null);
		User user = new User();
		user.setUserid(userid);
		user.setNickname(nickname);
		user.setGender(gender);
		user.setMood(mood);
		user.setInterest(interest);
		user.setCareer(career);
		int i = userManager.updateUser(user);
		JSONObject result = new JSONObject();
		if(i>0){
			result.put("success", 1);
			JSONResponseHelper.sendJSONResult(response, result, null);
			return;
		}else{
			result.put("success", 0);
			JSONResponseHelper.sendJSONResult(response, result, null);
		}
	}
	
	@RequestMapping(value = "/mobileapi/user/upload/signature")
	public void signature(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		//policy
		JSONObject result = new JSONObject();
		String policy = WebUtil.getStringPara(request, "policy");
		if(StringUtils.isBlank(policy)){
			result.put("success", 0);
			result.put("error", "no policy");
			JSONResponseHelper.sendJSONResult(response, result, null);
			return;
		}
		String signature = UpYunUtils.signature(policy + "&" + Constants.API_KEY);
		result.put("success", 1);
		result.put("signature", signature);
		result.put("policy", policy);
		JSONResponseHelper.sendJSONResult(response, result, null);
	}
}
